<template>
	<view class="seatStyle" @touchmove.stop="">
		<!-- <title :center="false" :bold="false" :title="cinemaData.cinema_name" iconSize="38" bgcolor="#FD9031" 
		 color="#FFF" @toclick="toBack(1)" icon="/static/img/other/to-back.png"></title> -->
		<title :center="false" :bold="false" :title="cinema_name" iconSize="38"
			bgcolor="#F5F6F8" color="#2E2E2E" @toclick="toBack(1)"
			icon="/static/img/other/to-back.png" :eleHeight="eleHeight + 'px'" boxSizing="content-box" ></title>
		<view class="white-modal" v-if="loading"></view>
		<view class="seat-group-box" :style="{'padding-top': height * 1.5 + 100 + eleHeight + 'rpx'}">
		<!-- 	<seat-box :cinema-info="cinemaData" :isZonePrice="isZonePrice" :maxChoose="4" :options='options'
				@bug="submitSeat"></seat-box> -->
				
			<seat-copy-box :cinema-info="cinemaData" :isZonePrice="isZonePrice" :maxChoose="4" :options='options'
				@bug="submitSeat"></seat-copy-box>
		</view>
		
		<!-- 是否调座弹框 start -->
		<seat-popup v-model="seatShow" @seat-cancel='seatPopupCancel' @seat-confirm='seatPopupConfirm'></seat-popup>
		<!-- 是否调座弹框 end -->
	</view>
</template>

<script>
	import title from '@/pages/components/title.vue'
	// import { seatList } from '@/public/seat.js'
	// import seatBox from '@/pages/components/seat.vue'
	import seatCopyBox from '@/pages/components/seat.vue'
	import SeatPopup from '../components/SeatPopup.vue'
	export default {
		data() {
			return {
				topHeight:'',
				eleHeight:'',
				seatShow: false,
				height: 0,
				loading: true,
				options: {},
				cinemaData: {
					seatList: []
				},
				selectSeat: [],
				is_order: '',
				isZonePrice: false,
				cinema_name:'',
			};
		},
		components: {
			title,
			// seatBox,
			SeatPopup,
			seatCopyBox
		},
		async onLoad(options) {
			let menu = uni.getMenuButtonBoundingClientRect()
			this.eleHeight = menu.height
			this.topHeight = this.$options.filters['pxChangeRpx'](menu.top,'rpx') 
			// console.log('this.topHeight',this.topHeight)
			// console.log('menu',menu)
			
			this.options = options
			this.cinema_name = this.$store.state.seatCopyInfo.cinema_name
			await this.checkOrder()
			
		},
		onShow() {
			uni.showLoading()
			this.$nextTick(() => {
				this.height = this.$common.setWidthHeight(() => {
					// this.getDom()
				})
			})
			this.loading = true
			this.cinemaData = {
				seatList: []
			}
			this.getSeatInfo()
		},
		methods: {
			seatPopupCancel(){
				this.addNewOrder(0)
			},
			seatPopupConfirm(){
				this.addNewOrder(1)
			},
			// 返回
			toBack(index) {
				this.$common.toBack(index)
			},
			// 获取影院座位信息
			getSeatInfo() {
				this.loading = true
				// 测试接口 start
				// this.$api.querySeats({
				// 	showId: decodeURIComponent(this.options.showId),
				// 	cinemaId: this.options.cinemaId,
				// 	toBackPrev: 2
				// }).then( res=> {
				// 	this.cinemaData = res.data.data
				// 	this.loading = false
				// 	uni.hideLoading()
				// })
				// return
				// 测试接口 end

				this.$api.getSeatInfo({
					no: decodeURIComponent(this.options.no),
					toBackPrev: 2
				}).then(res => {
					// 分区定价
					if (res.data.data.area_price != "") {
						let partition = JSON.parse(res.data.data.area_price)
						let length = Object.keys(partition).length
						if (length === 1) {
							this.isZonePrice = false
						} else {
							this.isZonePrice = true
						}
					} else {
						this.isZonePrice = false
					}
					this.cinemaData = res.data.data
					this.loading = false
					uni.hideLoading()
				})
			},
			// 提交座位信息
			submitSeat(seat) {
				this.selectSeat = seat
				this.seatShow = true
			},
			// 创建订单
			addNewOrder(type) {
				// uni.showLoading({
				// 	title: '加载中'
				// })
				let seat = [],
					seatId = [],
					seatType = []
				this.selectSeat.map(item => {
					seat.push(item.row + "排" + item.col + "座")
					seatId.push(item.id)
					seatType.push(item.type)
				})
				seatType = new Set(seatType)
				
				let one_area_id = this.cinemaData.seatList[0].area_id
				let one_wp_original_price = this.cinemaData.seatList[0].wp_original_price
				// original_price_flag : 是否分区
				let original_price_flag = this.cinemaData.seatList.every((item)=>{
					return item.area_id === one_area_id && item.wp_original_price === one_wp_original_price
				})
				// console.log('original_price_flag:',original_price_flag);
				let original_price = original_price_flag?this.selectSeat[0].wp_original_price:this.selectSeat[0].settle_price //area_id与settle_price不是都相同时的价格
				// console.log('价格',original_price);
				
				let order = {
					"cinemaName": this.cinemaData.cinema_name,
					"cname": this.cinemaData.cname,
					"tp": this.cinemaData.tp,
					"movieName": this.cinemaData.movieName,
					"hallName": this.cinemaData.hallName,
					"haveOrder": this.cinemaData.haveOrder,
					"show_time": this.cinemaData.showTime,
					"timeLength": this.cinemaData.timeLength,
					"tel": this.cinemaData.tel || this.$store.state.uInfo.tel,
					"mid": this.cinemaData.mid,
					"cinemaId": this.options.cinemaId,
					"settle_price": 0 === this.selectSeat.length ? 0 : this.selectSeat[0].settle_price,

					"price": 0 === this.selectSeat.length ? 0 : this.selectSeat[0].price,
					// "original_price": 0 === this.selectSeat.length ? 0 : this.selectSeat[0].wp_original_price,
					"original_price": 0 === this.selectSeat.length ? 0 : original_price,
					"count": this.selectSeat.length,
					"seatType": seatType.size == 1 ? 0 : seatType.size == 2 || seatType.size == 3 ? 1 : 0,
					"seat": seat.join(','),
					"seatId": seatId.join(','),
					"seqNo": decodeURIComponent(this.options.no),
					"ischange": type,
					"timeLength": this.options.timeLength,
					"cover_img":this.cinemaData.img
				}
				console.log('订单信息',order);
				uni.navigateTo({
					url: `/subpages/payment/payment?order=${encodeURIComponent(JSON.stringify(order))}`
				})
				
				// uni.navigateTo({
				// 	url: `/subpages/payment/payment_copy?order=${encodeURIComponent(JSON.stringify(order))}`
				// })
				
				// uni.navigateTo({
				// 	url: `/subpages/buyTicketMode/buyTicketMode?order=${encodeURIComponent(JSON.stringify(order))}`
				// })

			},
			// 获取待付款订单
			async checkOrder() {
				let _this = this
				let res = await this.$api.checkOrder()
				this.is_order = res.data.data.is_order
				if (this.is_order != '') {
					uni.showModal({
						title: "温馨提醒",
						content: "您有一笔未支付的订单，请及时处理",
						cancelColor: "#999",
						confirmColor: "#F1255D",
						cancelText: "取消订单",
						confirmText: "去支付",
						success: async function(res) {
							if (res.confirm) {
								uni.navigateTo({
									url: `/subpages/seat/submit?oid=${_this.is_order}&back=1`
								})
							} else if (res.cancel) {
								if (!await _this.getIsPayByOid()) {
									_this.$common.toBack(1)
								} else {
									_this.releaseOrder(true)
								}
							}
						},
					})
				}
			},
			// 获取订单是否支付
			async getIsPayByOid() {
				let res = await this.$api.getIsPayByOid({
					id: this.is_order
				})
				if (res.data.data.is_pay == 0) {
					return true
				} else {
					return false
				}
			},
			// 取消订单
			releaseOrder(status) {
				if (status) {
					uni.showLoading({
						title: "取消中"
					})
				}
				this.$api.releaseOrder({
					oid: this.is_order
				}).then(res => {
					this.getSeatInfo()
					// uni.hideLoading()
					// this.$common.toBack(1)
				}).catch(err => {
					console.log(err)
					this.$common.toBack(1)
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.seatStyle {
		background: #F5F6F8;
		.white-modal {
			position: fixed;
			top: 0;
			left: 0;
			width: 100%;
			height: 100vh;
			// background-color: #FFF;
			z-index: 1001;
		}

		.seat-group-box {
			height: 100vh;
			// background: linear-gradient(90deg, #f43f3b, #ec008c);
		}
	}
</style>
